import java.util.Arrays;

/**
 * @author ghp
 * @title
 */

class Solution {
    public void sortColors(int[] nums) {
        divide(nums, 0, nums.length - 1);
        System.out.println(Arrays.toString(nums));
    }

    private void divide(int[] nums, int l, int r) {
        if (l >= r) {
            return;
        }
        int mid = (r - l) / 2 + l;
        divide(nums, l, mid);
        divide(nums, mid + 1, r);
        merge(nums, l, mid, r);
    }

    private void merge(int[] nums, int l, int mid, int r) {
        int i = l;
        int j = mid + 1;
        int k = 0;
        int[] temp = new int[r - l + 1];
        while (i <= mid && j <= r) {
            if (nums[i] < nums[j]) {
                temp[k++] = nums[i++];
            } else {
                temp[k++] = nums[j++];
            }
        }
        while (i <= mid) {
            temp[k++] = nums[i++];
        }
        while (j<=r){
            temp[k++] = nums[j++];
        }
        for (int m = 0; m < k; m++) {
            nums[l+m] = temp[m];
        }
    }
}